//
//  OKViewController.swift
//  Community
//
//  Created by JKSD on 2019/8/5.
//  Copyright © 2019 ZKJ. All rights reserved.
//

import UIKit



class SWPTOKViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    
    let tableView = UITableView()
    var source = NSArray()
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return 1
    }
    
//    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        return 60
//    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as!OKTableViewCell
        
        cell.selectionStyle = UITableViewCell.SelectionStyle.none
        let model = self.source[indexPath.section] as! indexModel
        cell.fillCell(model: model)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UIView()
        header.frame = CGRect (x: 0, y: 0, width: SCREEN_WIDTH, height: 10)
        header.backgroundColor = UIColor.init(r: 242, g: 242, b: 242)
        return header
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.source.count
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super .viewWillAppear(animated)
        //        self.navigationItem.leftBarButtonItem = nil
        self.navigationItem.hidesBackButton = true
        
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "查询结果"
        let back = UIButton(type: UIButton.ButtonType.custom)
        back.frame = CGRect(x: 5,y: 20,width: 44,height: 44)
        back.setImage(UIImage.init(named: "black_return"), for: UIControl.State.normal)
        back.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
        back.addTarget(self, action: #selector(backAct), for: UIControl.Event.touchUpInside)
        self.navigationItem.leftBarButtonItem =  UIBarButtonItem.init(customView: back)
        self.tableView.frame = CGRect (x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - AllTopNavHeight)
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        self.tableView.register(UINib.init(nibName: "OKTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
        self.tableView.backgroundColor = UIColor.init(r: 242, g: 242, b: 242)
        self.view.addSubview(self.tableView)
        self.view.backgroundColor = UIColor.init(r: 242, g: 242, b: 242)
        
    }
    
    @objc func backAct() {
        self.navigationController?.popViewController(animated: true)
    }

   open func searchKey(keyWord:String)  {
    
            LYProgressHUD.showLoading("加载……")
            let str = "https://laji.lr3800.com/api.php?name=" + keyWord
            NetWorkRequest.sharedInstance.postWithRequest(UrlString: str, paramer: nil, success: { (obj) in
                LYProgressHUD.hide()
 
                if obj["msg"] as! String == "success"{
                    self.source = indexModel.mj_objectArray(withKeyValuesArray: obj["newslist"])
                    self.tableView.reloadData()
                }else{
                    LYProgressHUD.showMessage("没查到相关内容")
                    self.navigationController?.popViewController(animated: true)
                }
 
            }) { (error) in
                LYProgressHUD.hide()
                LYProgressHUD.showMessage("没查到相关内容")
                self.navigationController?.popViewController(animated: true)
            }
            
    
    }

    
}
